package com.demo.bookstore.dao.impl;

import com.demo.bookstore.bean.Book;
import com.demo.bookstore.dao.BookDao;

import java.util.List;

/**
 * @author ddone
 * @date 2024/3/14-20:53
 */
public class BookDaoImpl extends BaseDaoImpl implements BookDao {
    @Override
    public List<Book> findBookList() {
        String sql = "select id,title,author,price,sales,stock,img_path imgPath from book order by id desc";
        return this.getList(Book.class,sql);
    }

    @Override
    public int addBook(Book book) {
        String sql = "insert into book(title,author,price,sales,stock,img_path) values(?,?,?,?,?,?)";
        return this.update(sql,book.getTitle(),book.getAuthor(),book.getPrice(),book.getSales(),
        book.getStock(),book.getImgPath());
    }

    @Override
    public int deleteBook(int id) {
        String sql = "delete from book where id=?";
        return this.update(sql,id);
    }

    @Override
    public Book findBookById(int id) {
        String sql = "select id,title,author,price,sales,stock,img_path imgPath from book where id=?";
        return this.getBean(Book.class,sql,id);
    }

    @Override
    public int updateBook(Book book) {
        String sql = "update book set title=?,author=?,price=?,sales=?,stock=? where id=?";
        return this.update(sql,book.getTitle(),book.getAuthor(),book.getPrice(),book.getSales(),
                book.getSales(),book.getId());
    }
}
